package com.sxhuayuan.parking.compenent.shiro.realm;

import com.sxhuayuan.parking.compenent.domain.Setting.CaptchaType;
import com.sxhuayuan.parking.compenent.shiro.AccountType;
import com.sxhuayuan.parking.compenent.shiro.Principal;
import com.sxhuayuan.parking.entity.ParkingLotUser;
import com.sxhuayuan.parking.service.CaptchaService;
import com.sxhuayuan.parking.service.ParkingLotUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.pam.UnsupportedTokenException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 停车场用户登录验证
 * 
 * @author 何龙
 * @create 2019-04-19 9:25
 */
@Component("parkingLotAuthenticationRealm")
public class ParkingLotAuthenticationRealm extends AuthorizingRealm {

	@Resource(name = "captchaServiceImpl")
	private CaptchaService captchaService;

	@Resource(name = "parkingLotUserServiceImpl")
	private ParkingLotUserService parkingLotUserService;

	@Override
	public String getName() {
		return "parkinglot";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		return token instanceof com.sxhuayuan.parking.compenent.shiro.AuthenticationToken;
	}

	/**
	 * 获取认证信息(登录验证)
	 *
	 * @param token
	 *            令牌
	 * @return 认证信息
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		com.sxhuayuan.parking.compenent.shiro.AuthenticationToken authenticationToken = (com.sxhuayuan.parking.compenent.shiro.AuthenticationToken) token;

		String username = authenticationToken.getUsername();
		if (StringUtils.isEmpty(username) || authenticationToken.getPassword() == null)
			throw new IncorrectCredentialsException();
		String password = new String(authenticationToken.getPassword());
		String captchaId = authenticationToken.getCaptchaId();
		String captcha = authenticationToken.getCaptcha();
		String ip = authenticationToken.getHost();

		// 验证码
		if (!captchaService.isValid(CaptchaType.parkingLotLogin, captchaId, captcha)) {
			throw new UnsupportedTokenException();
		}

		ParkingLotUser parkingLotUser = parkingLotUserService.findByUsername(username);
		if (parkingLotUser == null) {
			throw new UnknownAccountException();
		}
		if (!parkingLotUser.getIsEnabled()) {
			throw new DisabledAccountException();
		}

		if (!password.equalsIgnoreCase(parkingLotUser.getPassword())) {
			throw new IncorrectCredentialsException();
		}

		// 更新登录信息
		parkingLotUser.setLoginIp(ip);
		parkingLotUser.setLoginDate(new Date());
		parkingLotUserService.update(parkingLotUser);
		return new SimpleAuthenticationInfo(new Principal(AccountType.parkinglot, parkingLotUser.getId(), username), password, getName());
	}

	/**
	 * 获取授权信息(访问权限)
	 *
	 * @param principals
	 *            principals
	 * @return 授权信息
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

}
